package action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.jboss.weld.context.ApplicationContext;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.opensymphony.xwork2.ActionContext;

import service.StaffService;
import vo.Staff;

/**
 * @author bohan
 * @create 2017年5月20日
 */
public class UserAction {
	StaffService service;
	Staff staff;
	JSONArray root;
	String key;
	int inputSize;
	List inputdata;
	
	public String login(){
		Map<String,Object> session = new HashMap<String, Object>();
		session =  ActionContext.getContext().getSession();
		session.clear();
		
		//从SecurityUtils里面创建一个subject  
		Subject subject = SecurityUtils.getSubject();  
        //在认证提交前，需要准备token(令牌)  
        UsernamePasswordToken token = new UsernamePasswordToken(staff.getStaffNo().toString(), staff.getPassword().toString()); 
        List list = new ArrayList();
        String result = "success";
        try {  
            //执行认证提交  
            subject.login(token);  
            System.out.println(subject.getPrincipal());
        } catch (UnknownAccountException e) {  
            result = "用户名不存在";
        }  catch (IncorrectCredentialsException e) {
			result = "密码不正确";
		}
        //是否认证通过  
        boolean isAuthenticated = subject.isAuthenticated();  
        System.out.println("是否认证通过：" + isAuthenticated);  
        
        if(isAuthenticated){
        	String name = service.findStaff(staff.getStaffNo().toString());
        	session.put("name", name);
        }
        
        list.add(result);
        root = JSONArray.parseArray(JSON.toJSONString(list,SerializerFeature.DisableCircularReferenceDetect));
		return "success";
	}
	
	public String logout(){
		Subject subject = SecurityUtils.getSubject();  
		boolean isAuthenticated = subject.isAuthenticated();  
        System.out.println("是否认证通过：" + isAuthenticated);  
        if(isAuthenticated==true){
        	try{
        	subject.logout(); 
	        }catch(Exception e){
	        	e.printStackTrace();
	        }
        }
        System.out.println("是否认证通过：" + subject.isAuthenticated());  
		return "success";
	}
	
	public String addStaff(){
		List list = new ArrayList();
		JSONArray input = JSONArray.parseArray(JSON.toJSONString(inputdata,false));
		JSONObject jso=JSON.parseObject(input.getString(0));
		System.out.println("jso"+jso);
		for(int i = 0 ; i < inputSize ; i++){
			staff.setGender(jso.getJSONObject(""+i+"").get("staff.gender").toString());
			staff.setPassword(jso.getJSONObject(""+i+"").get("staff.password").toString());
			staff.setName(jso.getJSONObject(""+i+"").get("staff.name").toString());
			staff.setRemark(jso.getJSONObject(""+i+"").get("staff.remark").toString());
			staff.setStaffNo(Integer.parseInt(jso.getJSONObject(""+i+"").get("staff.staffNo").toString()));
			staff.setTelephone(jso.getJSONObject(""+i+"").get("staff.telephone").toString());
			String role = jso.getJSONObject(""+i+"").get("staff.role").toString();
			list.add(service.addStaff(staff,role));
		}
	    root = JSONArray.parseArray(JSON.toJSONString(list,SerializerFeature.DisableCircularReferenceDetect));
		return "success";
	}
	
	public String updateStaff(){
		List list = new ArrayList();
		list.add(service.updateStaff(staff));
	    root = JSONArray.parseArray(JSON.toJSONString(list,SerializerFeature.DisableCircularReferenceDetect));
		return "success";
	}
	
	public String deleteStaff(){
		List list = new ArrayList();
		list.add(service.deleteStaff(staff));
	    root = JSONArray.parseArray(JSON.toJSONString(list,SerializerFeature.DisableCircularReferenceDetect));
		return "success";
	}
	
	
	public String findAllStaff(){
		List list = new ArrayList();
		list = service.findAllStaff();
	    root = JSONArray.parseArray(JSON.toJSONString(list,SerializerFeature.DisableCircularReferenceDetect));
		System.out.println(root);
	    return "success";
	}
	
	public StaffService getService() {
		return service;
	}
	public void setService(StaffService service) {
		this.service = service;
	}
	public Staff getStaff() {
		return staff;
	}
	public void setStaff(Staff staff) {
		this.staff = staff;
	}

	public JSONArray getRoot() {
		return root;
	}

	public void setRoot(JSONArray root) {
		this.root = root;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public int getInputSize() {
		return inputSize;
	}

	public void setInputSize(int inputSize) {
		this.inputSize = inputSize;
	}

	public List getInputdata() {
		return inputdata;
	}

	public void setInputdata(List inputdata) {
		this.inputdata = inputdata;
	}
	
}
